package com.neuralnoise.hibernate.liveJournal;

import static org.hibernate.criterion.Example.create;

import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.neuralnoise.hibernate.BaseHibernateDAO;

public class InterestDAO extends BaseHibernateDAO {
	
	private static final Logger log = LoggerFactory.getLogger(InterestDAO.class);

	public void save(Interest transientInstance) {
		log.debug("saving Interest instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Interest persistentInstance) {
		log.debug("deleting Interest instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Interest findById(Integer id) {
		log.debug("getting Interest instance with id: " + id);
		try {
			Interest instance = (Interest) getSession().get(Interest.class, id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Interest> findByExample(Interest instance) {
		log.debug("finding Interest instance by example");
		try {
			List<Interest> results = (List<Interest>) getSession()
					.createCriteria(Interest.class)
					.add(create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding LJInterest instance with property: " + propertyName + ", value: " + value);
		try {
			String queryString = "from LiveJournalInterest as model where model." + propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findAll() {
		log.debug("finding all Interest instances");
		try {
			String queryString = "from LiveJournalInterest";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Interest merge(Interest detachedInstance) {
		log.debug("merging Interest instance");
		try {
			Interest result = (Interest) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Interest instance) {
		log.debug("attaching dirty Interest instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Interest instance) {
		log.debug("attaching clean Interest instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}